package com.briup.ch08;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test2_List {
    public static void main(String[] args) {
        List<Integer> l1=new ArrayList<>();
        l1.add(10);//add新增
        l1.add(20);
        l1.add(30);
        l1.add(40);
        l1.set(0,11);//set只能修改
        //l1.set(4,50); set不能做新增
        l1.add(50);
        l1.add(1,18);//指定位置新增
        l1.add(6,60);
        //l1.add(10,100);//先size+1，然后插入元素
        System.out.println(l1);
        for (int i = 0; i < l1.size(); i++) {
            System.out.println(l1.get(i));
        }
        List sub=l1.subList(2,4);
        System.out.println(sub);//[2，4),左闭右开
        System.out.println(l1);
        System.out.println("------------------");
        //containsAll retainAll removeAll
        System.out.println("包含子集");
        System.out.println(l1.containsAll(sub));
        System.out.println(l1);
        System.out.println(sub);
        System.out.println("交集");
//        System.out.println(l1.retainAll(sub));
//        System.out.println(l1);//retainAll会改变原来的列表,让原来的列表只剩下交集
//        System.out.println(sub);//l1:只剩交集，sub没了
        List l2=new ArrayList<>(l1);
        System.out.println(l2.retainAll(sub));
        System.out.println(l1);
        System.out.println(sub);
        System.out.println(l2);
        System.out.println("差集");
        //System.out.println(l1.removeAll(sub));
        //System.out.println(l1);removeAll也会影响l1，让原来只剩下差集
        //System.out.println(sub);//l1:只剩差集，sub没了
        List l3=new ArrayList<>(l1);
        System.out.println(l3.removeAll(sub));
        System.out.println(l1);
        System.out.println(sub);
        System.out.println(l3);
        System.out.println("-------------------");
        l1.add(18);
        l1.add(40);
        System.out.println(l1);
        System.out.println(l1.indexOf(18));//从前向后
        System.out.println(l1.lastIndexOf(18));//从后向前
        System.out.println(l1.get(1));
        //get是根据索引找值，indexOf根据元素或者下标
        System.out.println("-----------");
        //循环
        System.out.println("普通版for循环");
        for (int i = 0; i < l1.size(); i++) {
            System.out.println(l1.get(i));
        }
        System.out.println("增强版for循环");
        for(int v:l1)
        {
            System.out.println(v);
        }
        System.out.println("迭代器");
        Iterator it=l1.iterator();
        while(it.hasNext())
        {
            //System.out.println(it.next());
            Object v=it.next();
            System.out.println(v);
        }
        l1.forEach(v-> System.out.println(v));
        l1.forEach(System.out::println);
    }
}
